package com.example.heimaai.config;

import com.example.heimaai.constant.SystemConstant;
import com.example.heimaai.tools.CourseTool;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.vectorstore.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiEmbeddingModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommonConfiguration {
    /**
     * Spring AI 默认提供了一个内存版的 ChatMemoryRepository，
     * 你需要先将它也声明为一个 Bean，这样其他 Bean 才能注入它。
     */
    @Bean
    public ChatMemoryRepository chatMemoryRepository() {
        return new InMemoryChatMemoryRepository();
    }

    @Bean
    public ChatMemory chatMemory(ChatMemoryRepository chatMemoryRepository) {
        return MessageWindowChatMemory.builder()
                .chatMemoryRepository(chatMemoryRepository) // 1. 提供必须的 Repository
                .build();                                      // 3. 构建实例
    }

    //    @Bean
//    //由OllamaChatModel替换为oepnai的模型
//    public ChatClient chatClient(OpenAiChatModel model, ChatMemory chatMemory,CourseTool courseTool) {
//        return ChatClient.builder(model)
////                .defaultSystem("你是一个热心、聪明的智能助手，你的名字叫嗓子哥")
////                替换成哄哄模拟器的专门提示词
//                .defaultSystem(SystemConstant.GAME_SYSTEM_PROMOPT)
//                .defaultAdvisors(
//                        new SimpleLoggerAdvisor(),
//                        MessageChatMemoryAdvisor.builder(chatMemory).build()
//                )
//                .defaultTools(courseTool)
//                .build();
//    }
    //定义智能客服的chatClient
//    @Bean
////由OllamaChatModel替换为oepnai的模型
//    public ChatClient chatClient(OpenAiChatModel model, ChatMemory chatMemory, CourseTool courseTool) {
//        return ChatClient.builder(model)
////                .defaultSystem("你是一个热心、聪明的智能助手，你的名字叫嗓子哥")
////                替换成哄哄模拟器的专门提示词
//                .defaultSystem(SystemConstant.SERVICE_SYSTEM_PROMOPT)
//                .defaultAdvisors(
//                        new SimpleLoggerAdvisor(),
//                        MessageChatMemoryAdvisor.builder(chatMemory).build()
//                )
//                .defaultTools(courseTool)
//                .build();
//    }
    //创建simpleVectorStore的bean，基于工厂
    @Bean
    public VectorStore vectorStore(OpenAiEmbeddingModel model){
        return SimpleVectorStore.builder(model).build();
    }
    //pdf的client
    @Bean
//由OllamaChatModel替换为oepnai的模型
    public ChatClient pdfChatClient(OpenAiChatModel model, ChatMemory chatMemory, VectorStore vectorStore) {
        return ChatClient.builder(model)
//                .defaultSystem("你是一个热心、聪明的智能助手，你的名字叫嗓子哥")
//                替换成哄哄模拟器的专门提示词
                .defaultSystem("根据上下文回答问题，没有的不要乱回答")
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        MessageChatMemoryAdvisor.builder(chatMemory).build(),
                        QuestionAnswerAdvisor.builder(vectorStore)
                        .searchRequest(SearchRequest.builder()
                                .similarityThreshold(0.6d).topK(2).build())
                       .build()
                )
                .build();
    }
}
